#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int n;
  cin >> n;
  
  ll sqsum = 0, sum = 0;
  vector<vector<int>> a(n);
  vector<int> pre(n);
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  for (int i = 0; i < n; i++) {
    int k, x;
    cin >> k;
    for (int j = 0; j < k; j++) {
      cin >> x;
      a[i].push_back(x);
    }
    sort(a[i].rbegin(), a[i].rend());
    pre[i] = a[i].back();
    sum += a[i].back();
    sqsum += 1ll * a[i].back() * a[i].back();
    a[i].pop_back();
    if (a[i].size()) {
      pq.push({a[i].back() + pre[i], i});
    }
  }

  ll ans = n * sqsum - sum * sum;
  while (!pq.empty()) {
    auto [v, id] = pq.top();
    pq.pop();
    v -= pre[id];
    sum += v - pre[id];
    sqsum += 1ll * v * v - 1ll * pre[id] * pre[id];
    a[id].pop_back();
    pre[id] = v;
    ans = min(ans, n * sqsum - sum * sum);
    if (a[id].size()) {
      pq.push({a[id].back() + pre[id], id});
    }
  }

  cout << ans << '\n';
  return 0;
}